/**
 * @author wonderful 20190813 1419
 * 主界面交互务逻辑控制
 */

(function(){

    var mainContent;
    var topBar;
    var leftBar;
    var threeMenuBar;
    var centerContent;
    var win;
    var topBarHeight=60;
    var leftBarWidth=150;

    function initDocs(){
       mainContent=$(".mainContent");
       topBar=$(".mainContent .topBar");
       leftBar=$(".mainContent .leftBar");
        threeMenuBar=$(".mainContent .threeMenuBar");
       centerContent=$(".mainContent .centerContent");
       win=$(window);
    }

   //重置布局尺寸
  function resizeLayout(){
      var leftBarHeight=win.height()-topBarHeight;
      mainContent.height(win.height());
      leftBar.height(leftBarHeight);
      centerContent.height(leftBarHeight);
      centerContent.width(win.width()-leftBarWidth-2);
      var progress=$(".mainContent .pageLoadProgress");
      if(progress.css("display")!="none"){
          pageLoad();
      }

  }

   //菜单折叠按钮点击事件
  function menuFoldBtnClick(){
        var delay=300;
        var menuFoldBtn=$(this);
        var isFolding=menuFoldBtn.data("isFolding");
        if(isFolding!=false&&!isFolding){
            menuFoldBtn.data("isFolding",true);
        }else if(isFolding==false){
            menuFoldBtn.data("isFolding",true);
        }else if(isFolding==true){
            return;
        }
        setTimeout(function(){
            menuFoldBtn.data("isFolding",false);
        },delay);
        if(menuFoldBtn.attr("class").indexOf("menuFold")==-1){
            leftBarWidth=60;
            menuFoldBtn.addClass("menuFold");
            menuFoldBtn.attr("title","展开左侧菜单");
            leftBar.animate({
                width:leftBarWidth
            },delay);
            leftBar.find(".menuBtn").animate({
                width:leftBarWidth
            },delay).find(".menuName").animate({
                opacity:0
            },delay);
            centerContent.animate({
                width:centerContent.width()+60
            },delay);

        }else{
            menuFoldBtn.removeClass("menuFold");
            menuFoldBtn.attr("title","折叠左侧菜单");
            leftBarWidth=120;
            leftBar.animate({
                width: leftBarWidth
            },delay);
            leftBar.find(".menuBtn").animate({
                width: leftBarWidth
            },delay).find(".menuName").animate({
                opacity:1
            },delay);
            centerContent.animate({
                width:centerContent.width()-60
            },delay);
        }

  }


  function leftMenuBtnClick(){
        var menuBtn=$(this);
        var menuRoute=menuBtn.data("menuRoute");
        var tId=menuBtn.data("tId");
        menuBtn.removeData("menuRoute");
        menuBtn.removeData("tId");
        threeMenuBar.find(".threeMmenuBtn").removeClass("threeMenuBtnFocus");//点击左边菜单时，隐藏三级菜单 yuanp 20191213
        threeMenuBar.find(".threeMmenuBtn ").remove();
        threeMenuBar.hide();
        leftBar.find(".menuBtn").removeClass("menuBtnFocus");
        topBar.find(".menuBtn").remove();
        menuBtn.addClass("menuBtnFocus");
        var menus=leftBar.data("menus");
        var curMenu=menuBtn.data("menu");
        if(curMenu.url&&curMenu.url!=""){
            var url=basePath+"/"+curMenu.url;
            if(tId){
                if(url.indexOf("?")!=-1){
                    url=url+"&tId="+tId;
                }else{
                    url=url+"?tId="+tId;
                }
            }
            pageLoad();
            $("#mainFrame").attr("src",url);
            $("#mainFrame").attr("scrolling", "auto");
            return;
        }

        for(var i=0;i<menus.length;i++){
            menu=menus[i];
            if(menu.parent&&menu.parent.id==curMenu.id){
                var menuBtn=$("<div class='menuBtn'></div>").appendTo(topBar);
                menuBtn.text(menu.name);
                menuBtn.data("menu",menu);
                menuBtn.bind("click",topMenuBtnClick);
                if(menuRoute&&menu.id==menuRoute.menuId){
                    if(menuRoute.nextMenu){
                        menuRoute.nextMenu.tId=menuRoute.tId;
                        menuBtn.data("menuRoute",menuRoute.nextMenu);
                    }else{
                        menuBtn.data("tId",menuRoute.tId);
                    }
                    menuBtn.click();
                }
            }
            threeMenuBar.find(".threeMmenuBtn").removeClass("threeMenuBtnFocus");
            threeMenuBar.find(".threeMmenuBtn ").remove();
            threeMenuBar.hide();

        }
        var topMenus=topBar.find(".menuBtn");
        if(!menuRoute&&topMenus.length>0){
            topMenus.eq(0).click();
        }


  }

  function topMenuBtnClick(){
        var menuBtn=$(this);
        var menuRoute=menuBtn.data("menuRoute");
        var tId=menuBtn.data("tId");
        menuBtn.removeData("menuRoute");
        menuBtn.removeData("tId");
        threeMenuBar.find(".threeMmenuBtn").removeClass("threeMenuBtnFocus");
        threeMenuBar.find(".threeMmenuBtn ").remove();
        threeMenuBar.hide();
        topBar.find(".menuBtn").removeClass("menuBtnFocus");
        menuBtn.addClass("menuBtnFocus");

        var curUserMenus=leftBar.data("menus");
        var curMenu=menuBtn.data("menu");

        var nextLvMenus=[];

        //在当前用户菜单中筛选出当前菜单的下级菜单
        for(var i=0;i<curUserMenus.length;i++){
            var menu=curUserMenus[i];
            if(menu.parent&&menu.parent.id==curMenu.id){
                nextLvMenus.push(menu);
            }
        }

      if(nextLvMenus.length>0){
      //当前菜单的下级菜单排序
      sortOrderMenus(nextLvMenus);

          setTimeout(function(){
              for(var i=0;i<nextLvMenus.length;i++){
                  menu=nextLvMenus[i];
                  threeMenuBar.show();
                  if(menu.parent&&menu.parent.id){
                      var threeMenuBtn=$("<div class='threeMmenuBtn'></div>").appendTo(threeMenuBar);
                      threeMenuBtn.text(menu.name);
                      threeMenuBtn.data("menu",menu);
                      threeMenuBtn.attr("href",basePath+"/"+menu.url);
                      threeMenuBtn.bind("click",threeMenuBtnClick);
                      if(menuRoute&&menu.id==menuRoute.menuId){
                          if(menuRoute.nextMenu){
                              menuRoute.nextMenu.tId=menuRoute.tId;
                              threeMenuBtn.data("menuRoute",menuRoute.nextMenu);
                          }else{
                              menuBtn.data("tId",menuRoute.tId);
                          }
                          threeMenuBtn.click();
                      }
                  }
              }

              if(!menuRoute){
                  threeMenuBar.find(".threeMmenuBtn").eq(0).click();
              }

              //访问第一个下级菜单
              // $("#mainFrame").attr("src", basePath+"/"+nextLvMenus[0].url);

          },100);
      }else{
          if(curMenu.url&&curMenu.url!=""){
              pageLoad();
              setTimeout(function(){
                  var url=basePath+"/"+curMenu.url;
                  if(tId){
                      if(url.indexOf("?")!=-1){
                          url=url+"&tId="+tId;
                      }else{
                          url=url+"?tId="+tId;
                      }
                  }
                  $("#mainFrame").attr("src",url);
                  $("#mainFrame").attr("scrolling", "auto");
              },100);
          }
      }
    }

    function threeMenuBtnClick() {
        var menuBtn=$(this);
        var menuRoute=menuBtn.data("menuRoute");
        var tId=menuBtn.data("tId");
        menuBtn.removeData("menuRoute");
        menuBtn.removeData("tId");
        threeMenuBar.find(".threeMmenuBtn").removeClass("threeMenuBtnFocus");
        menuBtn.addClass("threeMenuBtnFocus");

        var curUserMenus=threeMenuBar.data("menus");
        var curMenu=menuBtn.data("menu");
        var url=basePath+"/"+curMenu.url;
        if(tId){
            if(url.indexOf("?")!=-1){
                url=url+"&tId="+tId;
            }else{
                url=url+"?tId="+tId;
            }
        }
        if(curMenu.url.indexOf("http:")==-1){

        if($(".threeMenuBar").css("display")=="block"){
            pageLoad();
        }
            //访问第一个下级菜单
            $("#mainFrame").attr("src",url);
            $("#mainFrame").attr("scrolling", "auto");
        }else{
            window.open(curMenu.url);
        }

    }

  //返回1级菜单
  function getRootMenus(menu){
        if(menu.parent&&menu.parent.id!=1){
          return getRootMenus(menu.parent);
        }else{
            return menu;
        }
  }

  //菜单排序
  function sortOrderMenus(menus){
        for(var i=0;i<menus.length-1;i++){
          for(var y=0;y<menus.length;y++){
            var menu=menus[y];
            if(menus.length>(y+1)&&menu.orderNo>menus[y+1].orderNo){
                menus[y]=menus[y+1];
                menus[y+1]=menu;
            }
        }
        }
  }

  //初始化菜单
  function initLeftMenus(){
        var leftMenusCache={};
      $.ajax({
          url: basePath+"/menu/listUserMenu2",
          async: true,
          dataType: "json",
          data: {
              "pId": 1
          },
          type: "GET",
          contentType: "application/x-www-form-urlencoded",
          beforeSend: null,
          success: function (data) {
              if(data.status==SUCCESS){
                  var menus=data.data;
                  leftBar.data("menus",menus);
                  $(menus).each(function(i,menu){
                      //排除系统缺省菜单
                      if(menu.id==1){
                          return;
                      }
                      //获取一级菜单
                      var rootMenu=getRootMenus(menu);
                      //菜单去重
                      if(!leftMenusCache[rootMenu.id]) {
                          leftMenusCache[rootMenu.id] = rootMenu;
                      }
                  });

                  var sortMenus=[];
                  //菜单排序
                  for(var i in leftMenusCache){
                      var menu=leftMenusCache[i];
                      sortMenus.push(menu);
                  }
                  sortOrderMenus(sortMenus);

                  //绘制菜单
                  for(var i=0;i<sortMenus.length;i++){
                  var menu=sortMenus[i];
                  var menuBtn=$("<div class='menuBtn'></div>").appendTo(leftBar) ;
                  var menuName=$("<div class='menuName'></div>").appendTo(menuBtn);
                  menuBtn.addClass(SYSTEM_MENUBTNCLASS[menu.name]);
                  menuName.text(menu.name);
                  menuBtn.data("menu",menu);
                  menuBtn.bind("click",leftMenuBtnClick);

               }
              }
              leftBar.find(".menuBtn").eq(0).click();
          },
          error: function (XMLHttpRequest, textStatus, errorThrown) {


          }
      });


  }

  //页面加载
  function pageLoad(){

        var progress=$(".mainContent .pageLoadProgress");
        var centerContent=$(".mainContent .centerContent");
        var pos=centerContent.position();
        progress.show();
        progress.css({
           left:pos.left,
           top:pos.top,
           width:centerContent.width(),
           height:centerContent.height(),
           "z-index":9999
        });

  }

    //页面加载
    function pageLoaded(){
        var progress=$(".mainContent .pageLoadProgress");
        setTimeout(function(){ progress.hide();},1);

    }

    function loginOut(){
        setTimeout(function(){
            $.ajax({
                url:basePath+"/system/logout",
                //processData: false,
                //data: xmlDocument,
                async:true,
                dataType:"json",
                type: "POST",
                contentType:"application/x-www-form-urlencoded",
                beforeSend:null,
                success:function(data){
                    if (data.status == 1) {
                        window.parent.location.href=basePath+formateUrl("imp/system/login/login");
                    }
                },
                error:function (XMLHttpRequest, textStatus, errorThrown) {

                    $.wdfProgress.showMsg("服务器异常或网络超时！",$("body"),true);

                }
            });
        },500);

    }

$(document).ready(function(){
    initDocs();
    resizeLayout();
    $(window).bind("resize",resizeLayout);
    $(".mainContent .topBar .menuUnFold").bind("click",menuFoldBtnClick);
    initLeftMenus();
    $("#mainFrame").bind("load",function(){
       pageLoaded();
    });

    $(".mainContent .topBar .userInfoBox").bind("click",function(){
        var userInfoPanel=$(".mainContent .userInfoPanel");
        if(userInfoPanel.css("display")=="none"){
            userInfoPanel.show(300);
        }else{
            userInfoPanel.hide(300);
        }
            userInfoPanel.find(".logoutBtn").bind("click",function(){
            userInfoPanel.hide(300);
            loginOut();
        });
        userInfoPanel.find(".detailBtn").bind("click",function(){
            userInfoPanel.hide(300);
            topBar.find(".menuBtn").remove();
            threeMenuBar.find(".threeMmenuBtn ").remove();
            threeMenuBar.hide(300);
            // 跳转个人信息编辑页面
            $("#mainFrame").attr("src", basePath+"/dtt/main?url=imp/home/personal");
            $("#mainFrame").attr("scrolling", "auto");

        });
    });
    
    $("#app2").click(function () {
        pageLoad();
        $("#mainFrame").attr("src",basePath+"/dtt/main?url=imp/system/msg/msg");
        $("#mainFrame").attr("scrolling", "auto");
    })
    

});

})($);


function markReadMsg(id,messageType,tID,content,status) {

    var url = getForwarderDatas(messageType,tID,content);
    if(url){
        $("#mainFrame").attr("src",basePath+url);
        $("#mainFrame").attr("scrolling", "auto");
    }


    if(status == 1)
    {
        getUnReadMsgCount();
        return;
    }
    $.ajax({
        url :basePath+"/messageMgr/markReadMsg",
        //processData: false,
        //data: xmlDocument,
        async:true,
        dataType : "json",
        type : "POST",
        contentType : "application/x-www-form-urlencoded",
        data :{
            msgID:id
        },
        beforeSend : null,
        success : function(data) {

            getUnReadMsgCount();
//            $("#right_menu").hide();
//            $("#contentFrame").css("width","100%");
            // $("#contentFrame").attr("src",basePath+msgMenuFormat(messageType,content).url+"?frommsg=1&tID="+tID);
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {

        }
    });

}

//根据菜单链路对象数据跳转到指定页面
function cascadeMenuByMenuRoute(menuRoute){

    if(menuRoute.menuId){
        $(".mainContent .leftBar").find(".menuBtn").each(function(){
            var menu=$(this).data("menu");
            // alert(menu);
            if(menu&&menu.id==menuRoute.menuId){
                if(menuRoute.nextMenu){
                    menuRoute.nextMenu.tId=menuRoute.tId;
                    $(this).data("menuRoute",menuRoute.nextMenu);
                }else{
                    $(this).data("tId",menuRoute.tId);
                }
                $(this).click();
            }
        })
    }

}

